/*
 */

#include <avr/io.h>
#include "hd.h"
#include <util/delay.h>




int main(void)
{

    // Insert code
    devices_init();
    DDRE  = 0b10000001;
    PORTE |= 0x01;
    PORTF |= 0b00010000;
    uint8_t lTest1 = 0;
    volatile uint16_t k = 0;
    //volatile uint16_t j = 0;
#ifdef CC_RECEIVE
    CC_RX;
    lTest1 = 98;
#endif

    while(1)
    {
        //DDRF = 0X00;
        //PORTF = 0x00;
        volatile uint16_t i = 0;
        volatile uint16_t j = 0;

    #ifndef CC_RECEIVE
        //CcTransmit(gTxBuffer,TX_LENGTH);
        //CC_TX;
        //for (i = 0; i < 30000; i++)
        //{
            //for (j = 0; j < 16; j++);
        //}
        CC_IDLE;
        //CC_TX;
        //_delay_us(90);
        //lTest1 = CcStatus();
        CcTxClear();
        //lTest1 = CcRead(CCxxx0_TXBYTES);
        CcBurstWrite(CCxxx0_TXFIFO, gTxBuffer, TX_LENGTH);
        //lTest1 = CcRead(CCxxx0_TXBYTES);
        //i = 0;
        //j = 0;

        CC_TX;
        //if (PIND & 0x02)
        //{
           // lTest1 = 55;
        //}
        while (((PIND & 0x02) == 0) && (i < 1000))
        {
            i++;
        }
        if (i == 1000)
        {
            lTest1 = 99;
        }
        else
        {
            lTest1 = 88;
        }
        //_delay_us(255);
        /*if (PIND & 0x02)
        {
            lTest1 = 99;
        }
        else
        {
            lTest1 = 88;
        }*/
        //_delay_us(628);
        /*if (PIND & 0x02)
        {
            lTest1 = 77;
        }
        else
        {
            lTest1 = 66;
        }*/
        //i = 0;
        while ((PIND & 0x02) && (j < 10000))
        {
            j++;
        }
        if (j == 10000)
        {
            lTest1 = 77;
        }
        else if (j != 0)
        {
            lTest1 = 66;
        }
        //
        //lTest1 = CcStatus();
        //lTest1 = CcRead(CCxxx0_IOCFG0);
        //lTest1 = 55;
    #else

        CC_IDLE;
        CC_RX;

        _delay_us(90);
        lTest1 = CcStatus();
        //lTest1 = CcRead(CCxxx0_PKTCTRL0);
        //CcRxOn();
        //gRxBuffer[2] = 77;
        while (((PIND & 0x02) == 0) && (i < 10000))
        {
            i++;
        }
        if (i == 10000)
        {
            lTest1 = 88;
        }
        else
        {
            lTest1 = 77;
        }
        //if (PIND & 0x02)
        //{
            //i = 1;
            while ((PIND & 0x02) && (j < 10000))
            {
                j++;
            }
            if (j == 10000)
            {
                lTest1 = 66;
            }
            else
            {
                //CcReceive();
                CcBurstRead(CCxxx0_RXFIFO, gRxBuffer, RX_LENGTH);


                gRssi=CcRead(CCxxx0_RXFIFO);

                gLqi=CcRead(CCxxx0_RXFIFO);
                if (gLqi & 0x80) //80
                {
                    gCrcOk = 0x01;
                }
                else
                {
                    gCrcOk = 0x00;
                }
                CcRxClear();
                if (gCrcOk == 0x01)
                {
                    lTest1 = gRxBuffer[2];
                    k = gRxBuffer[2];
                    gCrcOk = 0x00;
                }
                else
                {
                    lTest1 = 55;
                }
                //CC_RX;
                //lTest1 = i;
            }
            if (k)
            {
                lTest1 = k;
            }
            //lTest1 = gRxBuffer[2];

        //}
        //else
        //{
            //lTest1 = 88;
        //}

        //lTest1 = k;
    #endif
        /*CC_SS_LOW;
        lTest1 = CcReset();
        CC_SS_HIGH;
        CcWrite(CCxxx0_FSCTRL1,  0x0b);
        lTest1 = CcRead(CCxxx0_FSCTRL1);*/
        NumberTurn(lTest1,1);
        /*ZERO_ON;
        LEL_ON;
        TWA_ON;
        RIGHT_ON;
        ALARM_ON;*/
        //DisplayAll();
        displayImmediately = 0x01;
        Display();
    }
    ;

    return 0;
}

